package com.example.tabhostdemo;

import java.util.ArrayList;
import java.util.List;

import android.app.LocalActivityManager;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.Toast;

/**
 * 可滑动的TabHost
 * 采用继承TabActivity方式
 * 
 */
@SuppressWarnings("deprecation")
public class SlideTabMainActivity extends TabActivity {

	protected static final String TAG = "SlideTabMainActivity";
	private TabHost tabHost;
	private ViewPager viewPager;
	private PagerTabStrip tabStrip; //设置每个标签页的标题
	private LocalActivityManager manager;
	private List<View> listView; //存放视图
	private List<String> titleList; //存放标题
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_slide_main);
		
		//get the instance of the TabHost
		tabHost = getTabHost();
		//viewPager替换FragmentLayout
		viewPager = (ViewPager) findViewById(R.id.viewPager);
		tabStrip = (PagerTabStrip) findViewById(R.id.pagertab);
		//设置标签页标题的样式
		tabStrip.setTextColor(Color.BLUE);
		//设置要不要添加添加一条内容和标题的分割线
		tabStrip.setDrawFullUnderline(false);
		//标签页标题的下划线
		tabStrip.setTabIndicatorColor(Color.RED);
		tabStrip.setBackgroundColor(Color.GRAY);
		titleList = new ArrayList<String>();
		titleList.add("第一页");
		titleList.add("第二页");
		titleList.add("第三页");
		
		manager = getLocalActivityManager();

		tabHost.addTab(tabHost
				.newTabSpec("first")
				.setIndicator("first")
				.setContent(
						new Intent(SlideTabMainActivity.this,
								FirstActivity.class)));
		tabHost.addTab(tabHost
				.newTabSpec("second")
				.setIndicator("second")
				.setContent(
						new Intent(SlideTabMainActivity.this,
								SecondActivity.class)));
		tabHost.addTab(tabHost
				.newTabSpec("third")
				.setIndicator("third")
				.setContent(
						new Intent(SlideTabMainActivity.this,
								ThirdActivity.class)));
		//设置默认显示标签页
		tabHost.setCurrentTab(0);
		tabHost.setOnTabChangedListener(new OnTabChangeListener() {
			
			//tabId是改变后显示标签的id
			@Override
			public void onTabChanged(String tabId) {
				if("first".equals(tabId)){
					viewPager.setCurrentItem(0);
				}else if("second".equals(tabId)){
					viewPager.setCurrentItem(1);
				}else if("third".equals(tabId)){
					viewPager.setCurrentItem(2);
				}
			}
		});
		
		listView = new ArrayList<View>();
		//适配器
		BasePagerAdapter adapter = new BasePagerAdapter(listView);
		Intent i = new Intent(this, FirstActivity.class);
		listView.add(manager.startActivity("first", i).getDecorView());
		i = new Intent(this, SecondActivity.class);
		listView.add(manager.startActivity("second", i).getDecorView());
		i = new Intent(this, ThirdActivity.class);
		listView.add(manager.startActivity("third", i).getDecorView());
		viewPager.setAdapter(adapter);
		viewPager.setOffscreenPageLimit(listView.size());
		viewPager.setCurrentItem(0);
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int position) {
				//Log.i(TAG, "onPageSelected------");
				tabHost.setCurrentTab(position);
			}
			
			@Override
			public void onPageScrolled(int position, float positionOffset,
					int positionOffsetPixels) {
				//Log.i(TAG, "onPageScrolled------");
			}
			
			@Override
			public void onPageScrollStateChanged(int state) {
				//Log.i(TAG, "onPageScrollStateChanged------");
			}
		});
	}
	
	class BasePagerAdapter extends PagerAdapter{
		
		private List<View> mlist;
		
		BasePagerAdapter(){}
		
		BasePagerAdapter(List<View> mlist){
			this.mlist = mlist;
		}

		@Override
		public int getCount() {
			return mlist.size();
		}

		@Override
		public boolean isViewFromObject(View view, Object obj) {
			return view == obj;
		}
		
		//必须复写
		@Override
		public Object instantiateItem(View container, int position) {
			Log.e(TAG, "--instantiateItem---" + position );
			((ViewPager)container).addView(mlist.get(position));
			return mlist.get(position);
		}
		
		//TODO 不知道这个什么时候执行
		@Override
		public void destroyItem(View container, int position, Object object) {
			Log.e(TAG, "--destroyItem---" + position + "--->" + object);
			((ViewPager)container).removeView(mlist.get(position));
		}
		
		/**
		 * 跳转到每个页面都要执行的方法，这个方法执行多次
		 * 	1a、点击页面时执行1次
		 *  1b、滑动到目标页面时执行2次
		 *  
		 *  2、如果点击tabhost时，跳转到目标页执行3次
		 */
		@Override
		public void setPrimaryItem(View container, int position, Object object) {
			Log.e(TAG, "__--" + position + "--->" + object);
		}
		
		//返回标签页的标题
		@Override
		public CharSequence getPageTitle(int position) {
			return titleList.get(position);
		}
		
	}

}
